/*jslint evil: true, plusplus: true, passfail: false, browser: true, devel: true, indent: 4, maxerr: 40, maxlen: 100 */

(function ($) {
    $.fn.rotateSliders = function (options) {
        var s, settings, me, addCircles, hexToRgb, colorNavCircles, settingsObj, slidingPanesGo;

        //defaults
        settings = $.extend({
            startRandom: false,
            animationDuration: 500,
            timePaneDisplays: 5500,
            width: "450px",
            height: "200px",
            circles: false,
            color: "#660707"
        }, options);

        me = this;

        hexToRgb = function (hex) {
            var shorthandRegex, result;
            // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
            shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
            hex = hex.replace(shorthandRegex, function (m, r, g, b) {
                return r + r + g + g + b + b;
            });

            result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
            return result ? {
                r: parseInt(result[1], 16),
                g: parseInt(result[2], 16),
                b: parseInt(result[3], 16)
            } : null;
        };
        colorNavCircles = function () {
            var convert, foo, count, makeStr, myVal;
            myVal = settings.color;

            count = 0;
            makeStr = 'rgba(';

            convert = hexToRgb(myVal);
            for (foo in convert) {
                if (convert.hasOwnProperty(foo)) {
                    count = count++;
                    makeStr += (convert[foo] + ',');
                }
            }

            makeStr += '.6)';
            settings.color = makeStr.toString();
        };

        addCircles = function () {
            if (settings.circles === true) {
                me.parent().find('ul').addClass('circles');
                colorNavCircles();
            }
        };

        //save the user from seizure
        if (settings.timePaneDisplays < 400) {
            settings.timePaneDisplays = 400;
        }
        if (settings.animationDuration >= settings.timePaneDisplays) {
            settings.timePanelDisplays = settings.animationDuration + 400;
        }

        settingsObj = {
            'height': settings.height,
            'width': settings.width
        };

        //panes
        this.children().css(settingsObj).css('float', 'left');

        s = {};
        s.timerOn = true;

        s.paneNum = this.children().length;
        s.paneWidth = this.children().eq(0).width();

        s.shiftPane = s.paneWidth * s.paneNum;
        s.myViewer = s.paneWidth;

        s.randomNumber = function () {
            return Math.floor(Math.random() * s.paneNum) * s.myViewer;
        };

        s.addNavigation = function () {
            var markup, i, len, num, posRight, navWidth, navAnimate, lower;
            len = s.paneNum;
            markup = '<ul class="numbers">';
            for (i = 0; i < len; i++) {
                num = i + 1;
                markup += '<li><a href="#">' + num + '</a></li>';
            }
            markup += '</ul>';

            me.wrap('<div class="widget"></div>');
            me.parent().css({
                'float': 'left',
                'position': 'relative',
                'overflow': 'hidden'
            });
            $(markup).prependTo(me.parent());

            addCircles();

            navWidth = 0;
            me.parent().find('li').each(function () {
                navWidth += $(this).outerWidth(true);
            });

            posRight = (s.myViewer - navWidth) / 2;

            me.parent().find('ul').css({
                'width': navWidth + 'px',
                'bottom': '10px',
                'right': posRight + 'px',
                'opacity': '0'
            });

            lower = me;

            navAnimate = function () {
                lower.parent().find('ul').animate({
                    'opacity': '1'
                }, 'swing');
            };

            setTimeout(function () {
                navAnimate();
            }, 1000);
        };

        this.wrapInner('<div class="shifter"></div>');
        s.addNavigation();

        //shifter
        this.children().css({
            'width': s.shiftPane,
            'position': 'relative'
        });

        this.css(settingsObj).css({
            'position': 'relative',
            'overflow': 'hidden'
        });

        s.pos = (settings.startRandom === true) ? s.randomNumber() : 0;
        s.timePaneDisplays = settings.timePaneDisplays;
        s.animationDuration = settings.animationDuration;

        //position for start
        me.children().css({
            'left': -s.pos
        });

        s.countDelay = function () {
            s.pos = s.pos + s.paneWidth;
            s.delay = setTimeout(s.changePane, s.timePaneDisplays);
        };

        s.animatePanel = function () {
            me.children().animate({
                left: -s.pos
            }, s.animationDuration, s.countDelay);
        };

        s.styleNavigation = function () {
            var currAnchor = s.pos / s.paneWidth;
            me.parent().find('a').removeClass('active').removeAttr('style');
            if (settings.circles === false) {
                settings.color = 'rgba(0,0,0,.8)';
            }
            me.parent().find('a').eq(currAnchor).addClass('active')
                .css('background-color', settings.color);
        };

        s.changePane = function () {
            if (s.timerOn) {
                if (s.pos < s.shiftPane) {
                    s.styleNavigation();
                    s.animatePanel();
                    clearTimeout(s.countDelay);
                } else {
                    s.pos = 0; //end of cyle begin again
                    s.styleNavigation();
                    s.animatePanel();
                }
            } else {
                s.doNothing = setTimeout(s.changePane, s.timePaneDisplays);
            }
        };

        slidingPanesGo = function (s) {
            s.styleNavigation();

            me.parent().find('a').mouseover(function () {
                s.timerOn = false;
            });
            me.parent().find('a').mouseout(function () {
                s.timerOn = true;
            });

            me.parent().find('a').each(function (i) {
                $(this).click(function () {
                    var pane;

                    setTimeout(function () {
                        s.setClickable = undefined;
                    }, s.animationDuration);

                    if (s.setClickable === undefined) {
                        pane = i + 1;
                        s.timerOn = false;
                        s.pos = s.paneWidth * pane;
                        s.pos = s.pos - s.paneWidth;
                        s.styleNavigation();
                        me.children().animate({
                            left: -s.pos
                        }, s.animationDuration);
                    }
                    s.setClickable = false;
                    return false;
                });
            });

            //start sliders
            s.changePane();
        };
        slidingPanesGo(s);
    };
}(jQuery));
